/**
 * @copyright   2010-2017, The Titon Project
 * @license     http://opensource.org/licenses/BSD-3-Clause
 * @link        http://titon.io
 */

@include export("grid") {
    $titon-grid-class-row: map-get($titon-grid, "class-row");
    $titon-grid-class-col: map-get($titon-grid, "class-column");
    $titon-grid-gutter: map-get($titon-grid, "gutter");
    $titon-grid-width: map-get($titon-grid, "width");
    $titon-grid-sizes: (
        "xs": (6, breakpoint-setting("xs", 1), breakpoint-setting("xs", 2)),
        "sm": (12, breakpoint-setting("sm", 1), breakpoint-setting("sm", 2)),
        "md": (12, breakpoint-setting("md", 1), breakpoint-setting("md", 2)),
        "lg": (12, breakpoint-setting("lg", 1), null), // Leave open ended
    );

    // Row wrapper
    #{generate-class-name($titon-grid, $titon-grid-class-row)} {
        @include grid-row($titon-grid-width);
    }

    // These should be the same for all sizes
    #{generate-class-name($titon-grid, $titon-grid-class-col)}[class*="span-"] {
        @include grid-column($titon-grid-width, $titon-grid-gutter);
    }

    // Global Push and pull classes
    @if map-get($titon-grid, "push-pull") {
        @for $i from 1 through map-get($titon-global, "span-count") {
            $span: grid-span($i, map-get($titon-global, "span-count"), $titon-grid-width, $titon-grid-gutter);

            .push-#{$i} {
                #{bidi-alignment()}: $span;
            }

            .pull-#{$i} {
                #{bidi-opposite-alignment()}: $span;
            }
        }
    }

    // Responsive sizes
    @each $size, $options in $titon-grid-sizes {
        $size-columns: nth($options, 1);

        @include in-range( (nth($options, 2), nth($options, 3)) ) {
            #{generate-class-name($titon-grid, $titon-grid-class-col)}[class*="#{$size}-"] {
                @include grid-column($titon-grid-width, $titon-grid-gutter);
            }

            @for $i from 1 through $size-columns {
                $span: grid-span($i, $size-columns, $titon-grid-width, $titon-grid-gutter);

                #{generate-class-name($titon-grid, $titon-grid-class-col)}.#{$size}-#{$i} {
                    width: $span;
                }

                @if map-get($titon-grid, "push-pull") {
                    .#{$size}-push-#{$i} {
                        #{bidi-alignment()}: $span;
                    }

                    .#{$size}-pull-#{$i} {
                        #{bidi-opposite-alignment()}: $span;
                    }
                }
            }
        }
    }
}
